﻿#region using...
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using NsMenu = Infusion.Silverlight.Controls.Menu;
#endregion

namespace Infusion.Silverlight.Controls.Ribbon
{
    #region : RibbonButtonGroupButton :
    #region Template Parts...
    [TemplatePart(Name = RibbonButtonGroupButton.RibbonButtonGroupButtonLayoutRoot, Type = typeof(Grid))]
    [TemplatePart(Name = RibbonButtonGroupButton.CanvasRibbonButtonIcon, Type = typeof(Canvas))]
    [TemplatePart(Name = RibbonButtonGroupButton.LineRightBorder, Type = typeof(Line))]
    [TemplatePart(Name = RibbonButtonGroupButton.ImgIcon, Type = typeof(Image))]
    [TemplatePart(Name = RibbonButtonGroupButton.CanvasRibbonButtonIconClickSurface, Type = typeof(Canvas))]
    [TemplatePart(Name = RibbonButtonGroupButton.CanvasRibbonButtonDropDown, Type = typeof(Canvas))]
    [TemplatePart(Name = RibbonButtonGroupButton.LineFarRightBorder, Type = typeof(Line))]
    [TemplatePart(Name = RibbonButtonGroupButton.ImgDropDown, Type = typeof(Image))]
    [TemplatePart(Name = RibbonButtonGroupButton.CanvasRibbonButtonDropDownClickSurface, Type = typeof(Canvas))]
    #endregion
    public class RibbonButtonGroupButton : Control
    {
        #region Events...
        public event ButtonClickEventHandler ButtonClick;
        #endregion

        #region TemplatePart Names...
        public const string RibbonButtonGroupButtonLayoutRoot = "RibbonButtonGroupButtonLayoutRoot";
        public const string CanvasRibbonButtonIcon = "CanvasRibbonButtonIcon";
        public const string LineRightBorder = "LineRightBorder";
        public const string ImgIcon = "ImgIcon";
        public const string CanvasRibbonButtonIconClickSurface = "CanvasRibbonButtonIconClickSurface";
        public const string CanvasRibbonButtonDropDown = "CanvasRibbonButtonDropDown";
        public const string LineFarRightBorder = "LineFarRightBorder";
        public const string ImgDropDown = "ImgDropDown";
        public const string CanvasRibbonButtonDropDownClickSurface = "CanvasRibbonButtonDropDownClickSurface";
        #endregion

        #region TemplatePart Variables...
        internal Grid ribbonButtonGroupButtonLayoutRoot;
        internal Canvas canvasRibbonButtonIcon;
        internal Line lineRightBorder;
        internal Image imgIcon;
        internal Canvas canvasRibbonButtonIconClickSurface;
        internal Canvas canvasRibbonButtonDropDown;
        internal Line lineFarRightBorder;
        internal Image imgDropDown;
        internal Canvas canvasRibbonButtonDropDownClickSurface;
        #endregion

        #region Dependeny Properties...
        #region MenuIDProperty
        /// <summary>
        /// Identifies the Infusion.Silverlight.Controls.Ribbon.RibbonButtonGroupButton.MenuID dependency property.
        /// </summary>
        public static readonly DependencyProperty MenuIDProperty = DependencyProperty.Register(
            "MenuID",
            typeof(string),
            typeof(RibbonButtonGroupButton),
            null);
        #endregion

        #region IsSelectedProperty
        /// <summary>
        /// Identifies the Infusion.Silverlight.Controls.Ribbon.RibbonButtonGroupButton.IsSelected dependency property.
        /// </summary>
        public static readonly DependencyProperty IsSelectedProperty = DependencyProperty.Register(
            "IsSelected",
            typeof(bool),
            typeof(RibbonButtonGroupButton),
            new PropertyMetadata(new PropertyChangedCallback(IsSelectedPropertyChanged)));
        #endregion

        #region IsToggleButtonProperty
        /// <summary>
        /// Identifies the Infusion.Silverlight.Controls.Ribbon.RibbonButtonGroupButton.IsToggleButton dependency property.
        /// </summary>
        public static readonly DependencyProperty IsToggleButtonProperty = DependencyProperty.Register(
            "IsToggleButton",
            typeof(bool),
            typeof(RibbonButtonGroupButton),
            null);
        #endregion
        #endregion

        #region Dependency Property Changed Handlers...
        #region IsSelectedPropertyChanged
        public static void IsSelectedPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            if (args.NewValue != null)
            {
                RibbonButtonGroupButton button = (RibbonButtonGroupButton)obj;

                if (!(bool)args.NewValue)
                {
                    if (button.canvasRibbonButtonIcon != null)
                        button.canvasRibbonButtonIcon.Background = Helper.GetBrush(LinearGradientBrushes.GroupedButtonBackground);
                }
                else
                {
                    if (button.canvasRibbonButtonIcon != null)
                        button.canvasRibbonButtonIcon.Background = Helper.GetBrush(LinearGradientBrushes.GroupedButtonSelected);
                }
            }
        }
        #endregion
        #endregion

        #region Class Variables...
        private bool templateApplied = false;
        private bool mouseOver = false;
        private RibbonButtonGroup parentRibbonButtonGroup;
        private string imageSource = string.Empty;
        private string toggleGroupID = string.Empty;
        private NsMenu.Menu menuObject = null;
        private Point menuLocation;
        #endregion

        #region Constructor(s)...
        #region RibbonButtonGroupButton
        public RibbonButtonGroupButton() : base()
        {
            DefaultStyleKey = typeof(RibbonButtonGroupButton);
        }
        #endregion
        #endregion

        #region Event Handlers...
        #region ribbonButtonGroupButtonLayoutRoot_LayoutUpdated
        private void ribbonButtonGroupButtonLayoutRoot_LayoutUpdated(object sender, EventArgs e)
        {

        }
        #endregion

        private void canvasRibbonButtonIconClickSurface_MouseEnter(object sender, MouseEventArgs e)
        {
            mouseOver = true;

            menuLocation = new Point(e.GetPosition(null).X - e.GetPosition(ribbonButtonGroupButtonLayoutRoot).X,
                e.GetPosition(null).Y - e.GetPosition(ribbonButtonGroupButtonLayoutRoot).Y + ActualHeight);

            if (!ParentRibbonButtonGroup.ParentRibbonTabGroup.ParentRibbonTab.ParentRibbon.HasMenuOpen)
            {
                if (!IsSelected)
                {
                    canvasRibbonButtonIcon.Background = Helper.GetBrush(LinearGradientBrushes.GroupedButtonHighlight);
                    lineRightBorder.Stroke = new SolidColorBrush(Helper.HexToColor("#C5C6C7"));
                    lineFarRightBorder.Stroke = new SolidColorBrush(Helper.HexToColor("#C5C6C7"));
                }
                else
                {
                    canvasRibbonButtonIcon.Background = Helper.GetBrush(LinearGradientBrushes.GroupedButtonHighlightAndSelected);
                    lineRightBorder.Stroke = new SolidColorBrush(Helper.HexToColor("#7D8587"));
                    lineFarRightBorder.Stroke = new SolidColorBrush(Helper.HexToColor("#7D8587"));
                }

                if (ButtonMenuStyle == ButtonMenuStyle.Split)
                {
                    canvasRibbonButtonDropDown.Background = Helper.GetBrush(LinearGradientBrushes.GroupedButtonSemiHighlight);
                }
            }
        }

        private void canvasRibbonButtonIconClickSurface_MouseLeave(object sender, MouseEventArgs e)
        {
            mouseOver = false;

            if (!ParentRibbonButtonGroup.ParentRibbonTabGroup.ParentRibbonTab.ParentRibbon.HasMenuOpen)
            {
                ResetVisualStyle();
            }
        }

        private void canvasRibbonButtonIconClickSurface_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            canvasRibbonButtonIcon.Background = Helper.GetBrush(LinearGradientBrushes.GroupedButtonHighlightAndSelected);
            canvasRibbonButtonDropDown.Background = Helper.GetBrush(LinearGradientBrushes.GroupedButtonSemiHighlight);
        }

        private void canvasRibbonButtonIconClickSurface_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (!ParentRibbonButtonGroup.ParentRibbonTabGroup.ParentRibbonTab.ParentRibbon.HasMenuOpen)
            {
                if (!string.IsNullOrEmpty(MenuID) && ButtonMenuStyle == ButtonMenuStyle.Simple)
                {
                    menuObject = ParentRibbonButtonGroup.ParentRibbonTabGroup.ParentRibbonTab.ParentRibbon.ShowMenu(MenuID, menuLocation, false);
                    menuObject.Closing += new EventHandler(menuObject_Closing);
                }
                else
                {
                    if (IsToggleButton)
                    {
                        bool switchButton = true;

                        if (parentRibbonButtonGroup != null)
                        {
                            if (parentRibbonButtonGroup.IsToggleGroup)
                            {
                                parentRibbonButtonGroup.ToggleAll(false);
                            }
                            else if (toggleGroupID != string.Empty)
                            {
                                int indexOfSelected = parentRibbonButtonGroup.ToggleGroup(toggleGroupID, false);
                                if (indexOfSelected == parentRibbonButtonGroup.Buttons.IndexOf(this))
                                    switchButton = false;
                            }
                        }

                        if (switchButton)
                            IsSelected = !IsSelected;
                    }

                    if (ButtonClick != null)
                        ButtonClick(this, e);

                    if (!IsSelected)
                    {
                        canvasRibbonButtonIcon.Background = Helper.GetBrush(LinearGradientBrushes.GroupedButtonHighlight);
                        lineRightBorder.Stroke = new SolidColorBrush(Helper.HexToColor("#C5C6C7"));
                        lineFarRightBorder.Stroke = new SolidColorBrush(Helper.HexToColor("#C5C6C7"));
                    }
                    else
                    {
                        canvasRibbonButtonIcon.Background = Helper.GetBrush(LinearGradientBrushes.GroupedButtonHighlightAndSelected);
                        lineRightBorder.Stroke = new SolidColorBrush(Helper.HexToColor("#7D8587"));
                        lineFarRightBorder.Stroke = new SolidColorBrush(Helper.HexToColor("#7D8587"));
                    }
                }
            }
        }

        private void canvasRibbonButtonDropDownClickSurface_MouseEnter(object sender, MouseEventArgs e)
        {
            mouseOver = true;

            menuLocation = new Point(e.GetPosition(null).X - e.GetPosition(ribbonButtonGroupButtonLayoutRoot).X,
                e.GetPosition(null).Y - e.GetPosition(ribbonButtonGroupButtonLayoutRoot).Y + ActualHeight);

            if (!ParentRibbonButtonGroup.ParentRibbonTabGroup.ParentRibbonTab.ParentRibbon.HasMenuOpen)
            {
                if (ButtonMenuStyle == ButtonMenuStyle.Split)
                {
                    canvasRibbonButtonDropDown.Background = Helper.GetBrush(LinearGradientBrushes.GroupedButtonHighlight);

                    if (!IsSelected)
                        canvasRibbonButtonIcon.Background = Helper.GetBrush(LinearGradientBrushes.GroupedButtonSemiHighlight);
                }
            }
        }

        private void canvasRibbonButtonDropDownClickSurface_MouseLeave(object sender, MouseEventArgs e)
        {
            mouseOver = false;

            if (!ParentRibbonButtonGroup.ParentRibbonTabGroup.ParentRibbonTab.ParentRibbon.HasMenuOpen)
            {
                if (ButtonMenuStyle == ButtonMenuStyle.Split)
                {
                    canvasRibbonButtonDropDown.Background = Helper.GetBrush(LinearGradientBrushes.GroupedButtonBackground);

                    if (!IsSelected)
                        canvasRibbonButtonIcon.Background = Helper.GetBrush(LinearGradientBrushes.GroupedButtonBackground);
                }
            }
        }

        private void canvasRibbonButtonDropDownClickSurface_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (!ParentRibbonButtonGroup.ParentRibbonTabGroup.ParentRibbonTab.ParentRibbon.HasMenuOpen)
            {
                if (ButtonMenuStyle == ButtonMenuStyle.Split)
                {
                    canvasRibbonButtonDropDown.Background = Helper.GetBrush(LinearGradientBrushes.GroupedButtonSelected);
                }
            }
        }

        private void canvasRibbonButtonDropDownClickSurface_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (!ParentRibbonButtonGroup.ParentRibbonTabGroup.ParentRibbonTab.ParentRibbon.HasMenuOpen)
            {
                if (!string.IsNullOrEmpty(MenuID))
                {
                    menuObject = ParentRibbonButtonGroup.ParentRibbonTabGroup.ParentRibbonTab.ParentRibbon.ShowMenu(MenuID, menuLocation, false);
                    menuObject.Closing += new EventHandler(menuObject_Closing);
                }
            }
        }

        private void menuObject_Closing(object sender, EventArgs e)
        {
            if (!mouseOver)
                ResetVisualStyle();

            if (menuObject != null)
            {
                menuObject.Closing -= new EventHandler(menuObject_Closing);
                menuObject = null;
            }
        }
        #endregion

        #region Methods...
        private void ResetVisualStyle()
        {
            if (!IsSelected)
            {
                canvasRibbonButtonIcon.Background = Helper.GetBrush(LinearGradientBrushes.GroupedButtonBackground);
                lineRightBorder.Stroke = new SolidColorBrush(Helper.HexToColor("#C5C6C7"));
                lineFarRightBorder.Stroke = new SolidColorBrush(Helper.HexToColor("#C5C6C7"));
            }
            else
            {
                canvasRibbonButtonIcon.Background = Helper.GetBrush(LinearGradientBrushes.GroupedButtonSelected);
                lineRightBorder.Stroke = new SolidColorBrush(Helper.HexToColor("#7D8587"));
                lineFarRightBorder.Stroke = new SolidColorBrush(Helper.HexToColor("#7D8587"));
            }

            if (ButtonMenuStyle == ButtonMenuStyle.Split)
            {
                canvasRibbonButtonDropDown.Background = Helper.GetBrush(LinearGradientBrushes.GroupedButtonBackground);
            }
        }

        #region OnApplyTemplate
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            if (!templateApplied)
            {
                ribbonButtonGroupButtonLayoutRoot = (Grid)GetTemplateChild(RibbonButtonGroupButtonLayoutRoot);
                canvasRibbonButtonIcon = (Canvas)GetTemplateChild(CanvasRibbonButtonIcon);
                lineRightBorder = (Line)GetTemplateChild(LineRightBorder);
                imgIcon = (Image)GetTemplateChild(ImgIcon);
                canvasRibbonButtonIconClickSurface = (Canvas)GetTemplateChild(CanvasRibbonButtonIconClickSurface);
                canvasRibbonButtonDropDown = (Canvas)GetTemplateChild(CanvasRibbonButtonDropDown);
                lineFarRightBorder = (Line)GetTemplateChild(LineFarRightBorder);
                imgDropDown = (Image)GetTemplateChild(ImgDropDown);
                canvasRibbonButtonDropDownClickSurface = (Canvas)GetTemplateChild(CanvasRibbonButtonDropDownClickSurface);

                ribbonButtonGroupButtonLayoutRoot.LayoutUpdated += new EventHandler(ribbonButtonGroupButtonLayoutRoot_LayoutUpdated);

                canvasRibbonButtonIconClickSurface.MouseEnter += new MouseEventHandler(canvasRibbonButtonIconClickSurface_MouseEnter);
                canvasRibbonButtonIconClickSurface.MouseLeave += new MouseEventHandler(canvasRibbonButtonIconClickSurface_MouseLeave);
                canvasRibbonButtonIconClickSurface.MouseLeftButtonDown += new MouseButtonEventHandler(canvasRibbonButtonIconClickSurface_MouseLeftButtonDown);
                canvasRibbonButtonIconClickSurface.MouseLeftButtonUp += new MouseButtonEventHandler(canvasRibbonButtonIconClickSurface_MouseLeftButtonUp);

                canvasRibbonButtonDropDownClickSurface.MouseEnter += new MouseEventHandler(canvasRibbonButtonDropDownClickSurface_MouseEnter);
                canvasRibbonButtonDropDownClickSurface.MouseLeave += new MouseEventHandler(canvasRibbonButtonDropDownClickSurface_MouseLeave);
                canvasRibbonButtonDropDownClickSurface.MouseLeftButtonDown += new MouseButtonEventHandler(canvasRibbonButtonDropDownClickSurface_MouseLeftButtonDown);
                canvasRibbonButtonDropDownClickSurface.MouseLeftButtonUp += new MouseButtonEventHandler(canvasRibbonButtonDropDownClickSurface_MouseLeftButtonUp);

                if (!string.IsNullOrEmpty(imageSource))
                    imgIcon.Source = new BitmapImage(new Uri(imageSource, UriKind.Relative));

                if (!string.IsNullOrEmpty(MenuID))
                {
                    imgDropDown.Visibility = Visibility.Visible;

                    if (ButtonClick != null)
                    {
                        //split menu button...
                        ribbonButtonGroupButtonLayoutRoot.ColumnDefinitions[1].Width = new GridLength(11);

                        canvasRibbonButtonDropDown.Visibility = Visibility.Visible;
                        canvasRibbonButtonDropDownClickSurface.Visibility = Visibility.Visible;
                    }
                    else
                    {
                        //menu button...
                        ribbonButtonGroupButtonLayoutRoot.ColumnDefinitions[0].Width = new GridLength(20);
                        ribbonButtonGroupButtonLayoutRoot.ColumnDefinitions[1].Width = new GridLength(10);

                        canvasRibbonButtonIcon.Width = 30;
                        canvasRibbonButtonIcon.SetValue(Grid.ColumnSpanProperty, 2);
                        canvasRibbonButtonIconClickSurface.Width = 30;
                        canvasRibbonButtonIconClickSurface.SetValue(Grid.ColumnSpanProperty, 2);

                        lineRightBorder.Visibility = Visibility.Collapsed;

                        imgIcon.HorizontalAlignment = HorizontalAlignment.Right;
                        imgIcon.Margin = new Thickness(0, 0, 1, 0);
                        imgDropDown.HorizontalAlignment = HorizontalAlignment.Left;
                        imgDropDown.Margin = new Thickness(1, 0, 0, 0);
                    }
                }

                if (IsSelected)
                    canvasRibbonButtonIcon.Background = Helper.GetBrush(LinearGradientBrushes.GroupedButtonSelected);

                templateApplied = true;
            }
        }
        #endregion
        #endregion

        #region Properties...
        #region ParentRibbonButtonGroup
        public RibbonButtonGroup ParentRibbonButtonGroup
        {
            get { return parentRibbonButtonGroup; }
            set
            {
                parentRibbonButtonGroup = value;

                if (parentRibbonButtonGroup.IsToggleGroup)
                {
                    IsToggleButton = true;
                    toggleGroupID = string.Empty;
                }
            }
        }
        #endregion

        #region ImageSource
        public string ImageSource
        {
            get { return imageSource; }
            set { imageSource = value; }
        }
        #endregion

        #region ToggleGroupID
        public string ToggleGroupID
        {
            get { return toggleGroupID; }
            set { toggleGroupID = value; }
        }
        #endregion

        #region ButtonMenuStyle
        public ButtonMenuStyle ButtonMenuStyle
        {
            get
            {
                ButtonMenuStyle menuStyle = ButtonMenuStyle.None;

                if (!string.IsNullOrEmpty(MenuID))
                {
                    if (ButtonClick != null)
                        menuStyle = ButtonMenuStyle.Split;
                    else
                        menuStyle = ButtonMenuStyle.Simple;
                }

                return menuStyle;
            }
        }
        #endregion

        #region MenuID
        public string MenuID
        {
            get { return (string)GetValue(MenuIDProperty); }
            set { SetValue(MenuIDProperty, value); }
        }
        #endregion

        #region IsSelected
        public bool IsSelected
        {
            get { return (bool)GetValue(IsSelectedProperty); }
            set { SetValue(IsSelectedProperty, value); }
        }
        #endregion

        #region IsToggleButton
        public bool IsToggleButton
        {
            get { return (bool)GetValue(IsToggleButtonProperty); }
            set { SetValue(IsToggleButtonProperty, value); }
        }
        #endregion
        #endregion
    }
    #endregion
}